package cn.hcbycloud.admin.notice;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * @author gaoh
 * @desc 自定义微服务告警通知
 * @date 2022年01月09日 下午 4:06
 */
@Slf4j
@Component
@SuppressWarnings("all")
public class ServiceNotifier extends AbstractEventNotifier {
    protected ServiceNotifier(InstanceRepository repository) {
        super(repository);
    }

    /**
     * 实现对事件的通知
     *
     * @param event
     * @param instance
     * @return
     */
    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> {
            // 判断实例状态是否发生了改变
            // instance.getRegistration().getName() 实例的名称
            // event.getInstance() 事件的实例
            // event.getType() 事件的类型
            if (event instanceof InstanceStatusChangedEvent) {

                log.info("Instance Status Change: [{}], [{}], [{}]",
                        instance.getRegistration().getName(), event.getInstance(),
                        ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
            } else {
                log.info("Instance Info: [{}], [{}], [{}]",
                        instance.getRegistration().getName(), event.getInstance(),
                        event.getType());
            }

        });
    }
}